package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertDataHandleDto;
import com.zzyl.dto.AlertDataPageQueryDto;
import com.zzyl.entity.AlertData;
import com.zzyl.entity.User;
import com.zzyl.mapper.AlertDataMapper;
import com.zzyl.service.AlertDataService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.AlertDataVo;
import com.zzyl.vo.AlertRuleVo;
import com.zzyl.vo.UserVo;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @Description AlertDataServiceImpl
 * @Author mks
 * @Date 2024-09-15
 */
@Service
public class AlertDataServiceImpl implements AlertDataService {

    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 分页查询报警数据
     * @param alertDataPageQueryDto
     * @return
     */
    @Override
    public PageResponse<AlertDataVo> pageQuery(AlertDataPageQueryDto alertDataPageQueryDto) {
        // 获取当前用户
       /* String subject = UserThreadLocal.getSubject();
        UserVo userVo = JSONUtil.toBean(subject, UserVo.class);*/
        Long userId = UserThreadLocal.getMgtUserId();
        // 开启分页
        PageHelper.startPage(alertDataPageQueryDto.getPageNum(),alertDataPageQueryDto.getPageSize());

        // 获取开始与结束时间
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;

        if(ObjectUtil.isNotEmpty(alertDataPageQueryDto.getStartTime())){
            startTime = LocalDateTimeUtil.of(alertDataPageQueryDto.getStartTime());
        }

        if(ObjectUtil.isNotEmpty(alertDataPageQueryDto.getEndTime())){
            endTime = LocalDateTimeUtil.of(alertDataPageQueryDto.getEndTime());
        }
        // 获取设备的名字
        String deviceName = alertDataPageQueryDto.getDeviceName();

        // 调用mapper层
        //Page<AlertDataVo> page = alertDataMapper.adminpageQuery(alertDataPageQueryDto.getId(),alertDataPageQueryDto.getStatus(),userVo.getId(),deviceName,startTime,endTime);
        Page<AlertDataVo> page = alertDataMapper.adminpageQuery(userId,deviceName,startTime,endTime);

        // 返回结果
        return PageResponse.of(page, AlertDataVo.class);
    }

    /**
     * 处理设备报警数据
     * @param alertDataHandleDto
     * @param id
     */
    @Override
    @Transactional
    public void handleAlertDataId(AlertDataHandleDto alertDataHandleDto, Long id) {

        /*// 获取当前用户
        String subject = UserThreadLocal.getSubject();
        User user = JSONUtil.toBean(subject, User.class);

        // 根据id查询报警数据
        AlertData alertData = alertDataMapper.selectByPrimaryKey(id);
        // 处理设备报警数据
        alertDataMapper.handleAlertData(id,
                alertDataHandleDto.getProcessingResult(),
                alertDataHandleDto.getProcessingTime(),
                user.getRealName(),
                user.getId(),
                alertData.getIotId());
        //
        String iotId = alertData.getIotId();
        Long userId = user.getId();
        String realName = user.getRealName();
        //
        alertDataMapper.updateStatusId(
                iotId,
                userId,
                realName,
                alertDataHandleDto.getProcessingResult(),
                alertDataHandleDto.getProcessingTime());*/


        // 获取当前用户的信息
        String subject = UserThreadLocal.getSubject();
        User user = JSONUtil.toBean(subject, User.class);

        // 根据id查询当前要处理的报警数据
        AlertData alertData = alertDataMapper.selectByPrimaryKey(id);

        // 基本信息的获取
        //String iotId = alertData.getIotId();
        Long alertRuleId = alertData.getAlertRuleId();
        Long userId = user.getId();
        String userRealName = user.getRealName();
        LocalDateTime processingTime = alertDataHandleDto.getProcessingTime();
        String processingResult = alertDataHandleDto.getProcessingResult();

        // 处理一条报警数据
        alertDataMapper.updateHandleAlertData(
                id,
                alertRuleId,
                processingResult,
                userId,
                userRealName,
                processingTime);
        // 如果该设备有多条报警待处理数据，此时有一条数据被处理，与此同时要更新其他待处理的该设备报警数据
        alertDataMapper.updateStatus(
                alertRuleId,
                processingResult,
                userId,
                userRealName,
                processingTime
        );
    }
}
